<?php

namespace app\admin\controller;

use app\admin\model\AdminMenu;
use think\facade\Validate;

use app\admin\model\Admin;
use app\admin\model\AdminRoleMenu;
use app\admin\model\Dictionary;
use app\admin\model\Mobile;

class LoginController {
    /*
     * 登录
     * author 崔
     * date 2022.03.25 09:01 
     * ------------------------------------------
     * update phper  2022-03-24 07:10 XXXXXXXXXXXXXXxxxx
     */

    public function login() {

        $admin = AdminMenu::where('parent_id','<>',0)->select()->toArray();
        foreach ($admin as $v){
            $admin1 = AdminMenu::where('id',$v['parent_id'])->find();
            if (empty($admin1)){
                AdminMenu::where('id',$v['id'])->delete();
            }
        }

        dump('11111111111111111111111');exit();
        $all = input('param.');
        $validate = Validate::rule([
                    'username|账号' => 'require',
                    'admin_pwd|密码' => 'require|min:6',
        ]);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, 'default');
        }
        $username = $all['username'];
        $admin_pwd = $all['admin_pwd'];
        $login_ip = getClientIp();

        $admin = new Admin();

        $row = $admin->where('username', '=', $username)
                //->where('password', '=', md5($admin_pwd))
                ->where('status', '=', Admin::STATUS_1)
                ->field('id,username,password,realname,avatar,role_id,salt,status')
                ->find();

        if(empty($row['status'])){
            return $this->ajaxReturn(3, '账号异常，如有疑问请联系管理员', 'default');
        }
        if($row['password'] != md5($admin_pwd.$row['salt'])){
            return $this->ajaxReturn(3, '密码错误', 'default');
        }
        unset($row['password']);
        unset($row['salt']);
        if ($row) {
            $token = signToken($row);
            $row['token'] = $token;
//            Cache::set("ADMIN_TOKEN_".$row['id'], $token,0);
            $data = [
                "last_login_time" => gD(),
                "last_login_ip" => $login_ip,
                "update_time" => gD(),
            ];

            $admin->where('id', '=', $row['id'])->update($data);
            //返回登录者的权限id
            $row['menu_id'] = AdminRoleMenu::where('role_id',$row['role_id'])->column('menu_id');
            return $this->ajaxReturn(100, '登录成功', $row);
        } else {
            return $this->ajaxReturn(3, '账号不存在', 'default');
        }
    }

     /*
     * 修改密码
     * author xfwed
     * date 2022.03.25 09:59
     * ------------------------------------------
     * update phper  2022-03-24 07:10 XXXXXXXXXXXXXXxxxx
     */
    public function savePwd() {
        $all = input('param.');
        $validate = Validate::rule([
                    'id|账号' => 'require',
                    'admin_pwd|密码' => 'require|min:6',
                    'new_admin_pwd|新密码' => 'require|min:6',
        ]);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, 'default');
        }
       $admin_pwd = $all['admin_pwd'];
       $row =  Admin::where('id',$all['id'])->find();
       if($row['password'] != md5($admin_pwd.$row['salt'])){
           return $this->ajaxReturn(3, '旧密码错误', 'default');
       }
       $save = Admin::where('id',$all['id'])
               ->update([
                   'password' => md5($all['new_admin_pwd'].$row['salt']),
                   'update_time' => gD(),
               ]);
        
       return $this->ajaxReturn(100, '修改成功', 'default');
    }
    
     /*
     * 返回json
     */

    public function ajaxReturn($code, $msg, $data) {
        $array = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,token");
        header('Access-Control-Allow-Methods: GET, POST, PUT');
        header('Content-Type:application/json; charset=utf-8');
        return json($array);
    }

}
